#! /usr/bin/env perl

###########################################################################
#
# vlog
#
###########################################################################
#
# This command prints the log messages for all revisions of the given file(s).  It pages the output to less or
# whatever $EDITOR is set to.  It also uses a temp file to avoid locking the repository if applicable.
#
# #########################################################################
#
# All the code herein is released under the Artistic License
#		( http://www.perl.com/language/misc/Artistic.html )
# Copyright (c) 2001-2009 Barefoot Software, Copyright (c) 2004-2006 ThinkGeek
#
###########################################################################

use strict;
use warnings;

use VCtools::Base;
use VCtools::Args;
use VCtools::Common;


#################################
# OPTIONS AND ENVIRONMENT VARS
#################################

my $DEFAULT_NUM = 100;

VCtools::switch('latest', 'l', 'show only latest log (same as -n1)');
VCtools::switch('number', 'n', "show only howmany logs (default: $DEFAULT_NUM)", 'howmany');
VCtools::switch('count', 'c', "don't show logs, just count them (useful with -U, not useful with -n)");
VCtools::switch('commit_user', 'U', 'show only logs from user', 'user');
VCtools::args('files', 'list', 'file(s) (or directory/ies) to show logs for');
VCtools::getopts();

# figure out how many logs to show
my $howmany = VCtools::latest() ? 1 : VCtools::number() || (VCtools::count() ? 0 : $DEFAULT_NUM);
print STDERR "number of logs to print: $howmany\n" if DEBUG >= 2;

# remember, directories are files too
my @files = VCtools::files();
print STDERR "log for files: @files\n" if DEBUG >= 3;


#################################
# CHECK FOR ERRORS
#################################

my $proj = VCtools::verify_files_and_group(@files);

# get statuses for everything at once (quicker that way)
VCtools::cache_file_status(@files, { DONT_RECURSE => 1 });

foreach my $file (@files)
{
	unless (VCtools::exists_in_vc($file))
	{
		VCtools::fatal_error("$file is not in VC") unless VCtools::ignore_errors();
		exit;
	}
}


#################################
# MAIN
#################################

my $count = @files;
foreach (@files)
{
	my $header_line = @files > 1 ? "\n---> $_ <---\n" : "";

	VCtools::get_log($_, $howmany, { AUTHOR => VCtools::commit_user() });
	if (VCtools::latest())
	{
		print $header_line, VCtools::log_lines(0) unless VCtools::pretend();
	}
	elsif (VCtools::count())
	{
		VCtools::verbose() ? VCtools::info_msg("found", VCtools::num_logs(), "logs") : print VCtools::num_logs(), "\n";
		exit 0;
	}
	else
	{
		VCtools::page_output('.vclog', $header_line, VCtools::log_lines()) unless VCtools::pretend();
		print "\n";
		VCtools::prompt_to_continue("$count more files to view") if --$count;
	}
}

print "\n";
VCtools::info_msg("stopped after printing", $howmany, "log messages;", "there may be more")
		if VCtools::num_logs() == $howmany;
print "done\n";


#################################
# SUBS
#################################
